package com.heima;

import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;

/**
 * ResultSet案例,將account账户数据封装成account对象，存放到ArrayList结合中
 */

public class JDBC_API_ResultSetDemo1 {
    public static void main(String[] args) throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取数据库连接
//        String url="mysql:jdbc://127.0.0.1:3306/user?useSSL=false";//连接本机主机名和端口号可以省略
        String url="jdbc:mysql:///user?useSSL=false";
        String username="root";
        String password="18307096730..";
        Connection conn = DriverManager.getConnection(url, username, password);

        //3.定义sql查询语句
        String sql="select *from account";

        //4.获取sql执行对象
        Statement stmt = conn.createStatement();

        //5.执行查询sql，使用executeQuery
        ResultSet rs = stmt.executeQuery(sql);

        //6.处理数据
        //创建account实体类

        //创建account类型的ArrayList集合
        ArrayList<account> list=new ArrayList<>();
        //遍历rs结果集将数据封装成account对象，并存放到list集合中
        while(rs.next()){
            int id = rs.getInt("id");
            String name = rs.getString("name");
            double money = rs.getDouble("money");
            //创建account对象
            account account=new account();
            account.setId(id);
            account.setName(name);
            account.setMoney(money);
            //将account对象存入list
            list.add(account);
        }

        System.out.println(list);
    }

}
